import { Shop } from './shop-model.js';
var shop = new Shop();
import { Config } from '../../utils/config.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusType: ["我的会员", "我的分销商"],
    currentTpye: 0,
    page: 1,
    hiddenNomore: true,
    hiddenLoading: true,
    hiddenNullData: true,
    onLoading: false,
    nomore: false,
    shop_id:'',
    id:'',
    code:''
  },
  onLoad: function (options) {
    this._getData();
  },
  /*获取店员*/
  _getData: function () {
    var that = this;
    var args = {
      page: this.data.page,
      num: 15,
      is_distribut: that.data.currentTpye,
      expand:1
    }
    that.setData({
      hiddenLoading: false
    })
    shop.user(args, (res) => {
      if (that.data.page == 1) {
        that.data.items = [];
      }
      var items = res.items;
      var items = that.data.items.concat(items);
      that.setData({
        items
      })
      //隐藏正在加载中
      that.setData({
        hiddenLoading: true
      })
      if (items.length == 0) {
        //显示没有数据
        that.setData({
          hiddenNullData: false
        })
      }
      if (that.data.page >= res._meta.pageCount) {
        that.data.nomore = true;
        if (that.data.page > 1) {
          //显示没有更多
          that.setData({
            hiddenNomore: false
          })
        }
      }
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.nomore) {
      return false;
    }
    this.data.page++;
    this._getData();
  },
  /**
   * 展示操作弹框
   */
  sheet: function (e) {
    var id  = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    this.data.code = e.currentTarget.dataset.code;
    this.data.shop_id = e.currentTarget.dataset.shop_id;
    if(!id){
      wx.showModal({
        title: '提示',
        content: '该数据有误',
      })
      return false;
    }
    this.data.id  = id;
    this.data.index = index;
    this.setData({
      showShare: true
    })
  },
  /**
  * 隐藏操作弹框
  */
  hiddenSheet: function () {
    this.setData({
      showShare: false
    })
  },
  /**
  * 编辑
  */
  edit: function () {
    wx.navigateTo({
      url: 'add-user?id='+this.data.id
    })
    this.hiddenSheet();
  },
  /**
  * 删除
  */
  del: function () {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您确定要删除该用户吗',
      success(res) {
        if (res.confirm) {
          var args ={
            id : that.data.id
          }
          shop.userDel(args, (res) => {
            that.hiddenSheet();
            if(res.status == 1 ){
              that.data.items.splice(that.data.index, 1);
              var data = that.data.items;
              that.setData({
                items : data
              })
              wx.showToast({
                title: '删除成功',
              })
            }else{
              wx.showModal({
                title: '提示',
                content: res.msg ? res.msg : '删除失败',
              })
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onShareAppMessage: function () {
    this.hiddenSheet();
    var path = '/page/shop/bind?p=' + this.data.code + "&id=" + this.data.shop_id;
    return {
      title: '邀请您来绑定我的店面',
      path: path,
      imageUrl: 'https://mini.888dcy.com/storage/shop-bind.jpg'
    }
  }
})